# -*- coding: utf-8 -*-
# @Project : fastapiDemo
# @File    : main.py
# @Date    : 2023-08-26
# uvicorn main:app --reload

from fastapi import FastAPI,Body,Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.security import HTTPBasic, HTTPBasicCredentials
from typing import Optional

# 创建一个APP实例
app = FastAPI()

app.mount("/static", StaticFiles(directory="static"), name="static")
templates = Jinja2Templates(directory="templates")
security = HTTPBasic()


# 添加路径操作装饰器和路径操作函数
@app.get("/")
def login_form(request: Request):
    return templates.TemplateResponse("login.html", {"request": request})

@app.post("/login")
# def login(credentials: HTTPBasicCredentials = security):
def login():
    if username == "admin" and password == "password":
        return {"Login": "Success"}
    else:
        return {"Login": "Failed"}

#重要代码，否者无法使用 Ctrl+C关闭服务
def handle_shutdown(signum, frame):
    sys.exit(0)

if __name__ == "__main__":
    import uvicorn
    # 启动服务，注意APP前面的文件名称
    uvicorn.run(app='main:app', host="127.0.0.1", port=8010, reload=True, debug=True)